// pages/components/homeitems/homeitems.js
const util = require('../../../utils/util.js');
const glob = getApp().globalData;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
      //标题内容
      title:{
          type:"String",
          value:''
      },
      //路演时间
      time:{
          type:'String',
          value:''
      },
      //发起人
      initiator:{
          type:'String',
          value:''
      },
      //是否正在演出
      showstate:{
          type:'String',
          value:'0'
      },
      //等待开始事件
      wait:{
          type:'String',
      },
      //是否订阅
      issubscription:{
          type:'String',
          value: '0'
      },
      //展示图片地址
      image:{
          type:"String",
      },
      detailstype:{
        type:"String",
        value:""
      }
  },
  /**
   * 组件的初始数据
   */
   data: {
       showWait:0,
       subdis:false,
       subdis2: false
   },
   attached: function () {
       if (this.data.showstate == "0") {
           let wait = this.data.wait;
           this.setData({
               showWait: util.getWaiting(wait)
           })
           let _this = this;
           var timer = setInterval(function () {
               _this._upDateWait(timer);
           }, 1000)
       }
   },
  /**
   * 组件的方法列表
   */
  methods: {
      //切换订阅状态
        sendSubscription:function(formid){
            let _this=this;
            this.setData({
                subdis:true
            })
            util.ajax(glob.ROOT_URL +"demost",{
                method:'post',
                header:{
                    "Accept": "application / vnd.dct.v1 + json"
                },
                data:{
                    uid:wx.getStorageSync('user_info').uid,
                    id:_this.id,
                    form_id: formid
                }
            },res=>{
                if(res.state==1){
                    _this.setData({
                        issubscription:'1',
                        subdis:false
                    })
                    wx.showToast({
                        title:'订阅成功',
                        icon:'none'
                    })
                }else{
                    _this.setData({
                        subdis: false
                    })
                    wx.showToast({
                        title: '订阅失败，请重试',
                        icon: 'none'
                    })
                }
            },err=>{
                _this.setData({
                    subdis: false
                })
                wx.showToast({
                    title: '订阅失败，网络错误',
                    icon: 'none'
                })             
            })         
        },
        previewImg:function(e){
          let url =  e.currentTarget.dataset.url;
          wx.previewImage({
            current: '', // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
          })
        },
        unSubscription:function(){
            let _this = this;
            this.setData({
                subdis2: true
            })
            util.ajax(glob.ROOT_URL + "demost", {
                method: 'delete',
                header: {
                    "Accept": "application / vnd.dct.v1 + json"
                },
                data: {
                    uid: wx.getStorageSync('user_info').uid,
                    id: _this.id
                }
            }, res => {
                if (res.state == 1) {
                    _this.setData({
                        issubscription: '0',
                        subdis2: false
                    })
                    wx.showToast({
                        title: '取消订阅成功',
                        icon: 'none'
                    })
                    _this.triggerEvent("unsub",{id:_this.id})
                } else {
                    _this.setData({
                        subdis2: false
                    })
                    wx.showToast({
                        title: '取消失败，请重试',
                        icon: 'none'
                    })
                }
            }, err => {
                _this.setData({
                    subdis2: false
                })
                wx.showToast({
                    title: '取消失败，网络错误',
                    icon: 'none'
                })
            })         
        },
        formSubmit:function (e) {
            console.log('form发生了submit事件，携带数据为：',e.detail.formId)
            this.sendSubscription(e.detail.formId)
        },
        noneClick:function(){
            
        },
        //跳转到详情
        hrefDetails:function(){
            let url = "";
            if (this.data.detailstype=="old"){
              url = '/pages/index/showdetails/showdetails?id=' + this.id + "&type=old";
            }else{
              url = '/pages/index/showdetails/showdetails?id=' + this.id;
            }
            wx.navigateTo({
                url: url
            })
        },
        //更新等待时间
        _upDateWait:function(timer){
            let oldtime = Number(this.data.wait);
            oldtime--;
            this.setData({
                wait:oldtime,
                showWait: util.getWaiting(oldtime)
            })
            if (oldtime <= 0) {
                oldtime = 0;
                this.setData({
                    showstate: "1"
                })
                clearInterval(timer);
            }
        }
    }
})
